<template>
  <div class="login-container">
    <el-card class="login-card">
      <div class="title-container">
        <h3 class="title">社区管理系统</h3>
      </div>

      <!-- 登录表单 -->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
      >
        <el-form-item prop="role">
          <el-radio-group v-model="loginForm.role" size="large">
            <el-radio-button label="Admin">管理员</el-radio-button>
            <el-radio-button label="Resident">居民</el-radio-button>
          </el-radio-group>
        </el-form-item>

        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="用户名"
            :prefix-icon="User"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="密码"
            :prefix-icon="Lock"
            show-password
            @keyup.enter="handleLogin"
          />
        </el-form-item>

        <el-form-item>
          <el-button
            :loading="loading"
            type="primary"
            class="submit-button"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import { User, Lock } from "@element-plus/icons-vue";
import { login, residentLogin } from "@/api/auth";

const router = useRouter();
const loading = ref(false);

// 登录表单
const loginFormRef = ref(null);
const loginForm = reactive({
  username: "",
  password: "",
  role: "Resident", // 默认为居民
});

// 登录表单验证规则
const loginRules = {
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 6, message: "密码长度不能小于6位", trigger: "blur" },
  ],
  role: [{ required: true, message: "请选择登录身份", trigger: "change" }],
};

// 处理登录
const handleLogin = () => {
  loginFormRef.value.validate(async (valid) => {
    if (valid) {
      try {
        loading.value = true;
        let res;
        if (loginForm.role === "Admin") {
          res = await login(loginForm);
        } else {
          res = await residentLogin(loginForm);
        }
        localStorage.setItem("token", res.token);
        // 存储用户信息
        localStorage.setItem("userInfo", JSON.stringify(res.user));

        ElMessage.success("登录成功");
        // 如果有重定向地址，则跳转到重定向地址
        if (loginForm.role === "Admin") {
          window.location.href = "/";
        } else {
          window.location.href = "/user";
        }
      } catch (error) {
        console.error("登录失败：", error);
      } finally {
        loading.value = false;
      }
    }
  });
};
</script>

<style lang="scss" scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f0f2f5;

  .login-card {
    width: 400px;
    padding: 20px;

    .title-container {
      text-align: center;
      margin-bottom: 30px;

      .title {
        font-size: 24px;
        color: #303133;
        margin: 0;
      }
    }

    .login-form {
      .el-form-item {
        margin-bottom: 25px;
      }

      .submit-button {
        width: 100%;
      }
    }
  }
}
</style>
